package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class AnchorListTask extends CouponHttpBaseTask {
    private int mSize;
    private int mPageNo;
    private String mType;
    private String mUserId;
    private String mSchoolName;
    private String mSubject;
    private static final String ANCHORLISTJSON = "hostList";
    private String mGrade;

    public AnchorListTask(int size, int pageNo, String userId, String schoolName, String subject, String type,String grade) {
        super(TaskType.ANCHOR_LIST_TASK);
        mSize = size;
        mPageNo = pageNo;
        mUserId = userId;
        mSchoolName = schoolName;
        mSubject = subject;
        mType = type;
        mGrade = grade;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
//        String resp = "{result:'AAAAAAA',hostList:{currentPage:1,pageCount:5,list:[{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':2,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小二','fansNum':3233,'isFocus':false},{'userId':3,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小三','fansNum':3235,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false}]}}";
//        JSONObject json = JSON.parseObject(resp);
        JSONObject json = response.getBody();
        int currentPage = 0;
        int pageCount = 0;
        int nextPage = 0;
        List<EatShowUserInfo> list = new ArrayList<EatShowUserInfo>();
        if (json != null) {
            JSONObject jsonString = json.getJSONObject(ANCHORLISTJSON);
            if (jsonString != null) {
                currentPage = jsonString
                        .getIntValue(ProtocolConst.CURRENT_PAGE);
                pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                list = JSON.parseArray(
                        jsonString.getString(ProtocolConst.LIST_TAG),
                        EatShowUserInfo.class);
            }

        }
        notifyTaskCompleted(nextPage, list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(
                    RequestType.ANCHOR_LIST_REQUEST);
            JSONObject json = new JSONObject();
            json.put("size", mSize);
            json.put("pageNo", mPageNo);
            json.put("userId", mUserId);
            json.put("schoolName",mSchoolName);
            json.put("subject",mSubject);
            json.put("type", mType);
            json.put("grade",mGrade);
            request.setData(json.toString());
            request.setApiNo(ApiNo.ANCHOR_LIST);
//            request.setApiNo("800100");
            addRequest(request);
        }
    }
}
